package com.wannengbang.cloudleader.service.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.wannengbang.cloudleader.R;
import com.wannengbang.cloudleader.bean.PlanListBean;
import com.wannengbang.cloudleader.service.PlanListActivity;
import com.wannengbang.cloudleader.utils.GlideUtils;
import com.wannengbang.cloudleader.utils.NumberFormatUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author: HWC
 * Description: 信用卡列表
 */
public class PlanListAdapter extends RecyclerView.Adapter<PlanListAdapter.ViewHolder> {


    private List<PlanListBean.DataBean> listsBeans;
    private Context context;
    private onCallBackListener onCallBackListener;

    public void setOnCallBackListener(PlanListAdapter.onCallBackListener onCallBackListener) {
        this.onCallBackListener = onCallBackListener;
    }

    public PlanListAdapter(List<PlanListBean.DataBean> listsBeans) {
        this.listsBeans = listsBeans;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        context = viewGroup.getContext();
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_my_plan_list, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(inflate);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        PlanListBean.DataBean dataBean = listsBeans.get(position);
        holder.tvNo.setText(dataBean.getCard_no());
        holder.tvDate.setText("计划日期：" + dataBean.getStart_date() + " ~ " + dataBean.getEnd_date());
        String money = NumberFormatUtils.formatDouble(Double.parseDouble(dataBean.getAmount()) / 100);
        holder.tvLimit.setText(money);
        holder.tvBankName.setText(dataBean.getBank_name());
        GlideUtils.loadImage(context, holder.ivPicture, dataBean.getBank_image());
        holder.tvRealName.setText(dataBean.getReal_name());
        holder.itemView.setOnClickListener(v -> {
            onCallBackListener.onCallBack(1, position);
        });
        if (dataBean.getRepayment_status() == 2) {
            holder.iv_plan_complete.setVisibility(View.VISIBLE);
        } else {
            holder.iv_plan_complete.setVisibility(View.GONE);
            holder.iv_plan_zuofei.setVisibility(View.GONE);
            if (Double.parseDouble(dataBean.getDelete_time()) > 0) {
                holder.iv_plan_zuofei.setVisibility(View.VISIBLE);
            } else {
                holder.iv_plan_zuofei.setVisibility(View.GONE);
            }
        }

        String completed = NumberFormatUtils.formatDouble(Double.parseDouble(dataBean.getAmount_completed()) / 100);
        String todo = NumberFormatUtils.formatDouble(Double.parseDouble(dataBean.getAmount_todo()) / 100);
        holder.tvCompleted.setText(completed);
        holder.tvUnCompleted.setText(todo);

    }

    @Override
    public int getItemCount() {
        return listsBeans.size() == 0 ? 0 : listsBeans.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_date)
        TextView tvDate;
        @BindView(R.id.iv_picture)
        ImageView ivPicture;
        @BindView(R.id.tv_bank_name)
        TextView tvBankName;
        @BindView(R.id.tv_no)
        TextView tvNo;
        @BindView(R.id.tv_real_name)
        TextView tvRealName;
        @BindView(R.id.tv_limit)
        TextView tvLimit;
        @BindView(R.id.iv_plan_complete)
        ImageView iv_plan_complete;
        @BindView(R.id.iv_plan_zuofei)
        ImageView iv_plan_zuofei;
        @BindView(R.id.tv_completed)
        TextView tvCompleted;
        @BindView(R.id.tv_unCompleted)
        TextView tvUnCompleted;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface onCallBackListener {
        void onCallBack(int type, int position);
    }
}
